{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import os\n",
    "from mask_functions import rle2mask\n",
    "from matplotlib import pyplot as plt\n",
    "import cv2\n",
    "\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# load the two reference submissions \n",
    "sub1 = pd.read_csv('submission/8523_0719_unet_512_seed3456_tta.csv.gz')\n",
    "sub2 = pd.read_csv('submission/8503_0719_unet_512_ensemble5seeds_tta.csv.gz')\n",
    "sub3 = pd.read_csv('submission/8385_0712_unet_512_seed1234.csv.gz')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ImageId</th>\n",
       "      <th>EncodedPixels</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.2.276.0.7230010.3.1.4.8323329.6160.151787519...</td>\n",
       "      <td>-1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.2.276.0.7230010.3.1.4.8323329.582.1517875163...</td>\n",
       "      <td>618672 2 2 8 1012 2 2 8 1000 30 994 30 994 32 ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.2.276.0.7230010.3.1.4.8323329.6985.151787520...</td>\n",
       "      <td>-1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.2.276.0.7230010.3.1.4.8323329.5865.151787519...</td>\n",
       "      <td>-1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1.2.276.0.7230010.3.1.4.8323329.6187.151787519...</td>\n",
       "      <td>225594 2 1022 2 1014 12 1012 12 1008 16 1008 1...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                             ImageId  \\\n",
       "0  1.2.276.0.7230010.3.1.4.8323329.6160.151787519...   \n",
       "1  1.2.276.0.7230010.3.1.4.8323329.582.1517875163...   \n",
       "2  1.2.276.0.7230010.3.1.4.8323329.6985.151787520...   \n",
       "3  1.2.276.0.7230010.3.1.4.8323329.5865.151787519...   \n",
       "4  1.2.276.0.7230010.3.1.4.8323329.6187.151787519...   \n",
       "\n",
       "                                       EncodedPixels  \n",
       "0                                                 -1  \n",
       "1  618672 2 2 8 1012 2 2 8 1000 30 994 30 994 32 ...  \n",
       "2                                                 -1  \n",
       "3                                                 -1  \n",
       "4  225594 2 1022 2 1014 12 1012 12 1008 16 1008 1...  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sub1.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ImageId</th>\n",
       "      <th>EncodedPixels</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.2.276.0.7230010.3.1.4.8323329.6160.151787519...</td>\n",
       "      <td>-1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.2.276.0.7230010.3.1.4.8323329.582.1517875163...</td>\n",
       "      <td>610480 2 16 12 994 2 16 12 992 8 4 22 990 8 4 ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.2.276.0.7230010.3.1.4.8323329.6985.151787520...</td>\n",
       "      <td>-1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.2.276.0.7230010.3.1.4.8323329.5865.151787519...</td>\n",
       "      <td>-1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1.2.276.0.7230010.3.1.4.8323329.6187.151787519...</td>\n",
       "      <td>260364 2 1022 2 13282 6 1018 6 1014 10 1014 10...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                             ImageId  \\\n",
       "0  1.2.276.0.7230010.3.1.4.8323329.6160.151787519...   \n",
       "1  1.2.276.0.7230010.3.1.4.8323329.582.1517875163...   \n",
       "2  1.2.276.0.7230010.3.1.4.8323329.6985.151787520...   \n",
       "3  1.2.276.0.7230010.3.1.4.8323329.5865.151787519...   \n",
       "4  1.2.276.0.7230010.3.1.4.8323329.6187.151787519...   \n",
       "\n",
       "                                       EncodedPixels  \n",
       "0                                                 -1  \n",
       "1  610480 2 16 12 994 2 16 12 992 8 4 22 990 8 4 ...  \n",
       "2                                                 -1  \n",
       "3                                                 -1  \n",
       "4  260364 2 1022 2 13282 6 1018 6 1014 10 1014 10...  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sub3.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1377, 2) (1377, 2) (1377, 2) (1377, 4)\n"
     ]
    }
   ],
   "source": [
    "# align indices\n",
    "sub1.columns = ['ImageId', 'enc1']\n",
    "sub2.columns = ['ImageId', 'enc2']\n",
    "sub3.columns = ['ImageId', 'enc3']\n",
    "\n",
    "sub = pd.merge(left=sub1, right=sub2, on='ImageId', how='inner')\n",
    "sub = pd.merge(left=sub, right=sub3, on='ImageId', how='inner')\n",
    "print(sub1.shape, sub2.shape, sub3.shape, sub.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0.14960058097313, 0.14451706608569354, 0.13507625272331156)"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(sub['enc1']!=sub['enc2']).mean(), (sub['enc1']!=sub['enc3']).mean(), (sub['enc2']!=sub['enc3']).mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.8424110384894699"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "((sub['enc1']=='-1')&(sub['enc2']=='-1')&(sub['enc3']=='-1')).mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([  90,   98,  130,  137,  164,  213,  216,  268,  348,  368,  383,\n",
       "         426,  501,  558,  562,  576,  582,  679,  714,  756,  813,  934,\n",
       "         951, 1012, 1052, 1114, 1125, 1154, 1159, 1367]),\n",
       " array([ 130,  213,  268,  346,  383,  558,  568,  713,  714,  756,  764,\n",
       "         797,  813,  826,  904,  917,  940, 1052, 1114, 1159, 1234, 1299,\n",
       "        1367]))"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# identify the positions where sub1 has empty predictions but sub2 does not\n",
    "idx1 = np.where((sub['enc1'] == '-1')&(sub['enc2'] != '-1'))[0]\n",
    "idx2 = np.where((sub['enc1'] == '-1')&(sub['enc3'] != '-1'))[0]\n",
    "#idx = np.concatenate([idx1, idx2])\n",
    "idx1, idx2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# is it really like missing mask?\n",
    "enc = 'enc3'\n",
    "i = idx2[11]\n",
    "\n",
    "mask = rle2mask(sub.loc[i, enc], 1024, 1024)\n",
    "mask = cv2.resize(mask, (512, 512))\n",
    "img_id = sub.loc[i, 'ImageId']\n",
    "img = plt.imread('data/processed/test/%s.png'%img_id)\n",
    "img = cv2.resize(img, (512, 512))\n",
    "\n",
    "plt.imshow(img, cmap=plt.cm.bone)\n",
    "plt.imshow(mask, alpha=0.3, cmap=\"Reds\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ImageId</th>\n",
       "      <th>enc1</th>\n",
       "      <th>enc2</th>\n",
       "      <th>enc3</th>\n",
       "      <th>EncodedPixels</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.2.276.0.7230010.3.1.4.8323329.6160.151787519...</td>\n",
       "      <td>-1</td>\n",
       "      <td>-1</td>\n",
       "      <td>-1</td>\n",
       "      <td>-1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.2.276.0.7230010.3.1.4.8323329.582.1517875163...</td>\n",
       "      <td>612538 4 2 12 1006 4 2 12 994 28 996 28 992 36...</td>\n",
       "      <td>620718 10 1014 10 1010 14 1010 14 1008 16 4 4 ...</td>\n",
       "      <td>610480 2 16 12 994 2 16 12 992 8 4 22 990 8 4 ...</td>\n",
       "      <td>612538 4 2 12 1006 4 2 12 994 28 996 28 992 36...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.2.276.0.7230010.3.1.4.8323329.6985.151787520...</td>\n",
       "      <td>-1</td>\n",
       "      <td>-1</td>\n",
       "      <td>-1</td>\n",
       "      <td>-1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.2.276.0.7230010.3.1.4.8323329.5865.151787519...</td>\n",
       "      <td>-1</td>\n",
       "      <td>-1</td>\n",
       "      <td>-1</td>\n",
       "      <td>-1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1.2.276.0.7230010.3.1.4.8323329.6187.151787519...</td>\n",
       "      <td>221506 4 1020 4 1014 10 1014 10 1010 14 1010 1...</td>\n",
       "      <td>-1</td>\n",
       "      <td>260364 2 1022 2 13282 6 1018 6 1014 10 1014 10...</td>\n",
       "      <td>221506 4 1020 4 1014 10 1014 10 1010 14 1010 1...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                             ImageId  \\\n",
       "0  1.2.276.0.7230010.3.1.4.8323329.6160.151787519...   \n",
       "1  1.2.276.0.7230010.3.1.4.8323329.582.1517875163...   \n",
       "2  1.2.276.0.7230010.3.1.4.8323329.6985.151787520...   \n",
       "3  1.2.276.0.7230010.3.1.4.8323329.5865.151787519...   \n",
       "4  1.2.276.0.7230010.3.1.4.8323329.6187.151787519...   \n",
       "\n",
       "                                                enc1  \\\n",
       "0                                                 -1   \n",
       "1  612538 4 2 12 1006 4 2 12 994 28 996 28 992 36...   \n",
       "2                                                 -1   \n",
       "3                                                 -1   \n",
       "4  221506 4 1020 4 1014 10 1014 10 1010 14 1010 1...   \n",
       "\n",
       "                                                enc2  \\\n",
       "0                                                 -1   \n",
       "1  620718 10 1014 10 1010 14 1010 14 1008 16 4 4 ...   \n",
       "2                                                 -1   \n",
       "3                                                 -1   \n",
       "4                                                 -1   \n",
       "\n",
       "                                                enc3  \\\n",
       "0                                                 -1   \n",
       "1  610480 2 16 12 994 2 16 12 992 8 4 22 990 8 4 ...   \n",
       "2                                                 -1   \n",
       "3                                                 -1   \n",
       "4  260364 2 1022 2 13282 6 1018 6 1014 10 1014 10...   \n",
       "\n",
       "                                       EncodedPixels  \n",
       "0                                                 -1  \n",
       "1  612538 4 2 12 1006 4 2 12 994 28 996 28 992 36...  \n",
       "2                                                 -1  \n",
       "3                                                 -1  \n",
       "4  221506 4 1020 4 1014 10 1014 10 1010 14 1010 1...  "
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sub.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [],
   "source": [
    "# map non-empty sub2,sub3... slots to empty ones in sub1\n",
    "sub['EncodedPixels'] = sub['enc1']\n",
    "sub['EncodedPixels'][idx1] = sub['enc2'][idx1]\n",
    "sub['EncodedPixels'][idx2] = sub['enc3'][idx2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [],
   "source": [
    "sub[['ImageId','EncodedPixels']].to_csv('submission/dirty_ensemble.csv.gz', index=False, compression='gzip')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#dirty ensemble LB=0.8471"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## voting on each pixel, [see here](https://www.kaggle.com/giuliasavorgnan/pneumothorax-models-ensemble-average)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.append('../')\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import os\n",
    "from glob import glob\n",
    "import sys\n",
    "#import skimage.measure\n",
    "import PIL\n",
    "from tqdm import tqdm, tqdm_notebook\n",
    "\n",
    "from dataset.mask_functions import rle2mask, mask2rle\n",
    "from matplotlib import pyplot as plt\n",
    "import cv2\n",
    "\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ensemble LB scores:\n",
    "- ensemble different model architectures helps!!!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "use how many subs:  5\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['../submission/6633_1115_deeplabv3plus_resnet_512x768_v13_seed2047-48-49-50-51.csv.gz',\n",
       " '../submission/6637_1116_unet_resnet34_384x576_v1_seed2080-81-82-83-84.csv.gz',\n",
       " '../submission/6671_1117_fpn_resnet34_576x768_v1_seed2120-21-22-23-24.csv.gz',\n",
       " '../submission/6650_1117_fpn_resnet34_576x768_v1_seed2150-51-52-53-54.csv.gz',\n",
       " '../submission/6561_1116_pspnet_resnet34_512x768_v1_seed2070-71-72-73-74.csv.gz']"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# read all submissions into daframes and store them in a list\n",
    "# use_subs = ['../submission/658_1027_deeplabv3plus_resnet_512x768_v4_seed4567.csv.gz',\n",
    "#             '../submission/656_1028_deeplabv3plus_resnet_512x768_v4_seed7890.csv.gz',\n",
    "#             '../submission/656_1028_deeplabv3plus_resnet_512x768_v4_seed8901.csv.gz'\n",
    "#            ] #LB=0.663, min_solutions=2, pos_ratio=0.428\n",
    "\n",
    "# use_subs = ['../submission/658_1027_deeplabv3plus_resnet_512x768_v4_seed4567.csv.gz',\n",
    "#             '../submission/656_1028_deeplabv3plus_resnet_512x768_v4_seed7890.csv.gz',\n",
    "#             '../submission/656_1028_deeplabv3plus_resnet_512x768_v4_seed8901.csv.gz',\n",
    "#             '../submission/655_1028_deeplabv3plus_resnet_512x768_v4_seed6789.csv.gz',\n",
    "#             '../submission/655_1028_deeplabv3plus_resnet_512x768_v4_seed9012.csv.gz'\n",
    "#            ] #LB=0.660, min_solutions=3, pos_ratio=0.435\n",
    "\n",
    "# use_subs = ['../submission/658_1027_deeplabv3plus_resnet_512x768_v4_seed4567.csv.gz',\n",
    "#             '../submission/656_1028_deeplabv3plus_resnet_512x768_v4_seed7890.csv.gz',\n",
    "#             '../submission/656_1028_deeplabv3plus_resnet_512x768_v4_seed8901.csv.gz',\n",
    "#             '../submission/655_1028_deeplabv3plus_resnet_512x768_v4_seed6789.csv.gz',\n",
    "#            ] #LB=0.658, min_solutions=2, pos_ratio=0.446\n",
    "\n",
    "# use_subs = ['../submission/658_1027_deeplabv3plus_resnet_512x768_v4_seed4567.csv.gz',\n",
    "#             '../submission/656_1028_deeplabv3plus_resnet_512x768_v4_seed7890.csv.gz',\n",
    "#             '../submission/656_1028_deeplabv3plus_resnet_512x768_v4_seed8901.csv.gz',\n",
    "#             '../submission/655_1028_deeplabv3plus_resnet_512x768_v4_seed9012.csv.gz',\n",
    "#            ] #LB=0.661, min_solutions=3, pos_ratio=0.419\n",
    "\n",
    "# use_subs = ['../submission/658_1027_deeplabv3plus_resnet_512x768_v4_seed4567.csv.gz',\n",
    "#             '../submission/657_1030_deeplabv3plus_resnet_512x768_v4_seed4321.csv.gz',\n",
    "#             '../submission/656_1028_deeplabv3plus_resnet_512x768_v4_seed8901.csv.gz'\n",
    "#            ] #LB=0.661, min_solutions=2, pos_ratio=0.426\n",
    "\n",
    "# use_subs = ['../submission/658_1027_deeplabv3plus_resnet_512x768_v4_seed4567.csv.gz',\n",
    "#             '../submission/657_1030_deeplabv3plus_resnet_512x768_v4_seed4321.csv.gz',\n",
    "#             '../submission/656_1028_deeplabv3plus_resnet_512x768_v4_seed8901.csv.gz',\n",
    "#             '../submission/656_1028_deeplabv3plus_resnet_512x768_v4_seed7890.csv.gz', \n",
    "#             '../submission/656_1101_deeplabv3plus_resnet_512x768_v5_seed8765.csv.gz'\n",
    "#            ] #LB=0.661, min_solutions=3, pos_ratio=0.428\n",
    "\n",
    "# use_subs = ['../submission/658_1027_deeplabv3plus_resnet_512x768_v4_seed4567.csv.gz',\n",
    "#             '../submission/657_1030_deeplabv3plus_resnet_512x768_v4_seed4321.csv.gz',\n",
    "#             '../submission/656_1028_deeplabv3plus_resnet_512x768_v4_seed7890.csv.gz', \n",
    "#            ] #LB=0.659, min_solutions=2, pos_ratio=0.429\n",
    "\n",
    "# use_subs = ['../submission/658_1027_deeplabv3plus_resnet_512x768_v4_seed4567.csv.gz',\n",
    "#             '../submission/657_1030_deeplabv3plus_resnet_512x768_v4_seed4321.csv.gz',\n",
    "#             '../submission/kaggler0_submission_segmentation_and_classifier.csv'\n",
    "#            ] #LB=0.663, min_solutions=2, pos_ratio=0.430\n",
    "\n",
    "# use_subs = ['../submission/661_1103_deeplabv3plus_resnet_512x768_v6_seed2001.csv.gz',\n",
    "#             '../submission/658_1103_deeplabv3plus_resnet_512x768_v6_seed2003.csv.gz',\n",
    "#             '../submission/658_1027_deeplabv3plus_resnet_512x768_v4_seed4567.csv.gz'\n",
    "#            ] #LB=0.659, min_solutions=2, pos_ratio=0.431\n",
    "\n",
    "# use_subs = ['../submission/661_1103_deeplabv3plus_resnet_512x768_v6_seed2001.csv.gz',\n",
    "#             '../submission/659_1108_deeplabv3plus_resnet_512x768_v6_seed2009.csv.gz',\n",
    "#             '../submission/659_1108_deeplabv3plus_resnet_512x768_v6_seed2010.csv.gz'\n",
    "#            ] #LB=0.663, min_solutions=2, pos_ratio=0.417\n",
    "\n",
    "# use_subs = ['../submission/661_1103_deeplabv3plus_resnet_512x768_v6_seed2001.csv.gz',\n",
    "#             '../submission/6614_1109_deeplabv3plus_resnet_512x768_v10_seed2021-4ch-empty-fix.csv.gz',\n",
    "#             '../submission/658_1027_deeplabv3plus_resnet_512x768_v4_seed4567.csv.gz'\n",
    "#            ] #LB=0.6642, min_solutions=2, pos_ratio=0.426\n",
    "\n",
    "# use_subs = ['../submission/661_1103_deeplabv3plus_resnet_512x768_v6_seed2001.csv.gz',\n",
    "#             '../submission/6614_1109_deeplabv3plus_resnet_512x768_v10_seed2021-4ch-empty-fix.csv.gz',\n",
    "#             '../submission/658_1027_deeplabv3plus_resnet_512x768_v4_seed4567.csv.gz', \n",
    "#             '../submission/659_1108_deeplabv3plus_resnet_512x768_v6_seed2009.csv.gz',\n",
    "#             '../submission/659_1108_deeplabv3plus_resnet_512x768_v6_seed2010.csv.gz'\n",
    "#            ] #LB=0.6635, min_solutions=3, pos_ratio=0.420; LB=.6635, min_solutions=2, pos_ratio=.448\n",
    "\n",
    "# use_subs = ['../submission/661_1103_deeplabv3plus_resnet_512x768_v6_seed2001.csv.gz',\n",
    "#             '../submission/6614_1109_deeplabv3plus_resnet_512x768_v10_seed2021-4ch-empty-fix.csv.gz',\n",
    "#             '../submission/658_1027_deeplabv3plus_resnet_512x768_v4_seed4567.csv.gz', \n",
    "#             '../submission/655_1112_fpn_resnet34_512x768_v1_seed2034-35-36-37-38.csv.gz',\n",
    "#             '../submission/655_1112_fpn_resnet34_512x768_v1_seed2034.csv.gz'\n",
    "#            ] #LB=0.6643, min_solutions=3, pos_ratio=0.42; LB=., min_solutions=2, pos_ratio=.\n",
    "\n",
    "# use_subs = ['../submission/661_1103_deeplabv3plus_resnet_512x768_v6_seed2001.csv.gz',\n",
    "#             '../submission/6614_1109_deeplabv3plus_resnet_512x768_v10_seed2021-4ch-empty-fix.csv.gz',\n",
    "#             '../submission/658_1027_deeplabv3plus_resnet_512x768_v4_seed4567.csv.gz', \n",
    "#             '../submission/6577_1114_fpn_resnet34_1024x1536_v1_seed2043-2044-2045.csv.gz',\n",
    "#             '../submission/655_1112_fpn_resnet34_512x768_v1_seed2034.csv.gz'\n",
    "#            ] #LB=0.6655, min_solutions=3, pos_ratio=.42; LB=.6656, min_solutions=2, pos_ratio=.458\n",
    "\n",
    "# use_subs = ['../submission/6633_1115_deeplabv3plus_resnet_512x768_v13_seed2047-48-49-50-51.csv.gz',\n",
    "#             '../submission/6574_1115_unet_resnet34_512x768_v1_seed2060-61-62.csv.gz',\n",
    "#             '../submission/6577_1114_fpn_resnet34_1024x1536_v1_seed2043-2044-2045.csv.gz'\n",
    "#            ]#LB=.6685, min_solutions=2, pos_ratio=.408\n",
    "\n",
    "# use_subs = ['../submission/6633_1115_deeplabv3plus_resnet_512x768_v13_seed2047-48-49-50-51.csv.gz',\n",
    "#             '../submission/6637_1116_unet_resnet34_384x576_v1_seed2080-81-82-83-84.csv.gz',\n",
    "#             '../submission/6577_1114_fpn_resnet34_1024x1536_v1_seed2043-2044-2045.csv.gz'\n",
    "#            ]#LB=.6698, min_solutions=2, pos_ratio=.409\n",
    "\n",
    "# use_subs = ['../submission/6633_1115_deeplabv3plus_resnet_512x768_v13_seed2047-48-49-50-51.csv.gz',\n",
    "#             '../submission/6637_1116_unet_resnet34_384x576_v1_seed2080-81-82-83-84.csv.gz',\n",
    "#             '../submission/6671_1117_fpn_resnet34_576x768_v1_seed2120-21-22-23-24.csv.gz'\n",
    "#            ]#LB=.6705, min_solutions=2, pos_ratio=.404\n",
    "\n",
    "use_subs = ['../submission/6633_1115_deeplabv3plus_resnet_512x768_v13_seed2047-48-49-50-51.csv.gz',\n",
    "            '../submission/6637_1116_unet_resnet34_384x576_v1_seed2080-81-82-83-84.csv.gz',\n",
    "            '../submission/6671_1117_fpn_resnet34_576x768_v1_seed2120-21-22-23-24.csv.gz', \n",
    "            '../submission/6650_1117_fpn_resnet34_576x768_v1_seed2150-51-52-53-54.csv.gz', \n",
    "            '../submission/6561_1116_pspnet_resnet34_512x768_v1_seed2070-71-72-73-74.csv.gz'\n",
    "           ]#LB=.6718, min_solutions=3, pos_ratio=.400\n",
    "\n",
    "# use_subs = ['../submission/6633_1115_deeplabv3plus_resnet_512x768_v13_seed2047-48-49-50-51.csv.gz',\n",
    "#             '../submission/6637_1116_unet_resnet34_384x576_v1_seed2080-81-82-83-84.csv.gz',\n",
    "#             '../submission/6671_1117_fpn_resnet34_576x768_v1_seed2120-21-22-23-24.csv.gz', \n",
    "#             '../submission/6650_1117_fpn_resnet34_576x768_v1_seed2150-51-52-53-54.csv.gz', \n",
    "#             '../submission/6561_1116_pspnet_resnet34_512x768_v1_seed2070-71-72-73-74.csv.gz', \n",
    "#             '../submission/6574_1115_unet_resnet34_512x768_v1_seed2060-61-62.csv.gz',\n",
    "#             '../submission/6614_1109_deeplabv3plus_resnet_512x768_v10_seed2021-4ch-empty-fix.csv.gz'\n",
    "#            ]#LB=.6698, min_solutions=4, pos_ratio=.402\n",
    "\n",
    "print('use how many subs: ', len(use_subs))\n",
    "df_sub_list = [pd.read_csv(f).fillna('') for f in use_subs]#.set_index(['Image_Label'])\n",
    "use_subs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.0\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([0.20458087, 0.20763448, 0.2319729 , 0.21726253, 0.13854923])"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "diversity = 0.5 #0~1, the larger number, larger diversity in weights, defualt=1\n",
    "weights = np.array([int(name.split('/')[2].split('_')[0])-6500 for name in use_subs], dtype=int)**diversity / 200\n",
    "weights = weights / weights.sum()\n",
    "print(weights.sum())\n",
    "weights\n",
    "\n",
    "#weights = None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Image_Label</th>\n",
       "      <th>EncodedPixels_0</th>\n",
       "      <th>EncodedPixels_1</th>\n",
       "      <th>EncodedPixels_2</th>\n",
       "      <th>EncodedPixels_3</th>\n",
       "      <th>EncodedPixels_4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10055ff.jpg_Fish</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10055ff.jpg_Flower</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10055ff.jpg_Gravel</td>\n",
       "      <td>110 11 125 123 417 217 760 232 1099 251 1447 2...</td>\n",
       "      <td>1587 1 1593 3 1603 4 1930 2 1933 7 1942 4 1952...</td>\n",
       "      <td>1207 50 1553 67 1887 102 2236 104 2585 106 293...</td>\n",
       "      <td>522 33 857 68 1203 90 1550 95 1870 2 1887 114 ...</td>\n",
       "      <td>99 17 127 120 441 165 607 27 785 203 1124 228 ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10055ff.jpg_Sugar</td>\n",
       "      <td>3 184 201 4 215 129 352 345 701 347 1051 347 1...</td>\n",
       "      <td>54 1 56 4 65 2 68 1 73 3 77 1 84 1 88 1 261 1 ...</td>\n",
       "      <td>7 134 251 77 355 148 590 96 704 154 937 104 10...</td>\n",
       "      <td>5 136 242 90 353 150 586 103 702 155 929 113 1...</td>\n",
       "      <td>1 135 258 87 351 142 600 98 701 146 945 104 10...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>6e54759.jpg_Fish</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          Image_Label                                    EncodedPixels_0  \\\n",
       "0    10055ff.jpg_Fish                                                      \n",
       "1  10055ff.jpg_Flower                                                      \n",
       "2  10055ff.jpg_Gravel  110 11 125 123 417 217 760 232 1099 251 1447 2...   \n",
       "3   10055ff.jpg_Sugar  3 184 201 4 215 129 352 345 701 347 1051 347 1...   \n",
       "4    6e54759.jpg_Fish                                                      \n",
       "\n",
       "                                     EncodedPixels_1  \\\n",
       "0                                                      \n",
       "1                                                      \n",
       "2  1587 1 1593 3 1603 4 1930 2 1933 7 1942 4 1952...   \n",
       "3  54 1 56 4 65 2 68 1 73 3 77 1 84 1 88 1 261 1 ...   \n",
       "4                                                      \n",
       "\n",
       "                                     EncodedPixels_2  \\\n",
       "0                                                      \n",
       "1                                                      \n",
       "2  1207 50 1553 67 1887 102 2236 104 2585 106 293...   \n",
       "3  7 134 251 77 355 148 590 96 704 154 937 104 10...   \n",
       "4                                                      \n",
       "\n",
       "                                     EncodedPixels_3  \\\n",
       "0                                                      \n",
       "1                                                      \n",
       "2  522 33 857 68 1203 90 1550 95 1870 2 1887 114 ...   \n",
       "3  5 136 242 90 353 150 586 103 702 155 929 113 1...   \n",
       "4                                                      \n",
       "\n",
       "                                     EncodedPixels_4  \n",
       "0                                                     \n",
       "1                                                     \n",
       "2  99 17 127 120 441 165 607 27 785 203 1124 228 ...  \n",
       "3  1 135 258 87 351 142 600 98 701 146 945 104 10...  \n",
       "4                                                     "
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sub_merged = None\n",
    "\n",
    "for idx, df_sub in enumerate(df_sub_list):\n",
    "    df_sub.rename(columns={'EncodedPixels': 'EncodedPixels_%d'%idx}, inplace=True)\n",
    "    if sub_merged is None:\n",
    "        sub_merged = df_sub\n",
    "    else:\n",
    "        sub_merged = sub_merged.merge(df_sub, on=['Image_Label'])\n",
    "\n",
    "sub_merged.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.6\n"
     ]
    }
   ],
   "source": [
    "## Create average prediction mask for each image ##\n",
    "# set here the threshold for the final mask\n",
    "# min_solutions is the minimum number of times that a pixel has to be positive in order to be included in the final mask\n",
    "\n",
    "#min_solutions = 4 # if avg_mask>=min_solutions, then predict a pixel=1\n",
    "min_solutions = 0.6\n",
    "\n",
    "print(min_solutions)\n",
    "# assert (min_solutions >= 1 and min_solutions <= len(df_sub_list)), \\\n",
    "#     \"min_solutions has to be a number between 1 and the number of submission files\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "def vote_for_mask(rles, min_solutions, weights=None):\n",
    "    \"\"\"\n",
    "    \"\"\"\n",
    "    if weights is None:\n",
    "        weights = [1]*len(rles)\n",
    "    vote_mask = np.zeros((350, 525))\n",
    "    for i,rle in enumerate(rles):#N models\n",
    "        if str(rle)!='':\n",
    "            mask = rle2mask(rle, (525, 350))\n",
    "            #mask = (mask>0.5).astype(np.float32)\n",
    "            vote_mask += (mask*weights[i])\n",
    "        else:#if empty, not add to overall\n",
    "            pass\n",
    "    pred_mask = (vote_mask >= min_solutions).astype(np.uint8)\n",
    "    if pred_mask.sum() > 0:\n",
    "        return mask2rle(pred_mask)\n",
    "    else:\n",
    "        return ''\n",
    "\n",
    "# idx = 8\n",
    "# rles = sub_merged.loc[idx, ][1:].tolist()\n",
    "# mask_voted = vote_for_mask(rles, min_solutions)\n",
    "# mask_voted"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "#plt.imshow(rle2mask(sub_merged.loc[1100, 'EncodedPixels'], (525, 350)))\n",
    "#plt.imshow(rle2mask(sub_merged.loc[105, 'EncodedPixels']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "c499a7b4df8449728666f0e32a8a54a4",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(IntProgress(value=0, max=14792), HTML(value='')))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "5000\n",
      "10000\n",
      "\n",
      "CPU times: user 1min, sys: 136 ms, total: 1min\n",
      "Wall time: 1min 1s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "res = []\n",
    "for idx in tqdm_notebook(sub_merged.index):\n",
    "    rles = sub_merged.loc[idx, ][1:].tolist()\n",
    "    mask_voted = vote_for_mask(rles, min_solutions, weights)\n",
    "    res.append(mask_voted)\n",
    "    \n",
    "    if idx%5000==0:\n",
    "        print(idx)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Image_Label</th>\n",
       "      <th>EncodedPixels_0</th>\n",
       "      <th>EncodedPixels_1</th>\n",
       "      <th>EncodedPixels_2</th>\n",
       "      <th>EncodedPixels_3</th>\n",
       "      <th>EncodedPixels_4</th>\n",
       "      <th>ensemble</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10055ff.jpg_Fish</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10055ff.jpg_Flower</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10055ff.jpg_Gravel</td>\n",
       "      <td>110 11 125 123 417 217 760 232 1099 251 1447 2...</td>\n",
       "      <td>1587 1 1593 3 1603 4 1930 2 1933 7 1942 4 1952...</td>\n",
       "      <td>1207 50 1553 67 1887 102 2236 104 2585 106 293...</td>\n",
       "      <td>522 33 857 68 1203 90 1550 95 1870 2 1887 114 ...</td>\n",
       "      <td>99 17 127 120 441 165 607 27 785 203 1124 228 ...</td>\n",
       "      <td>1207 50 1553 67 1887 102 2236 104 2585 106 293...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10055ff.jpg_Sugar</td>\n",
       "      <td>3 184 201 4 215 129 352 345 701 347 1051 347 1...</td>\n",
       "      <td>54 1 56 4 65 2 68 1 73 3 77 1 84 1 88 1 261 1 ...</td>\n",
       "      <td>7 134 251 77 355 148 590 96 704 154 937 104 10...</td>\n",
       "      <td>5 136 242 90 353 150 586 103 702 155 929 113 1...</td>\n",
       "      <td>1 135 258 87 351 142 600 98 701 146 945 104 10...</td>\n",
       "      <td>7 134 251 77 355 148 586 103 704 154 929 113 1...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>6e54759.jpg_Fish</td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          Image_Label                                    EncodedPixels_0  \\\n",
       "0    10055ff.jpg_Fish                                                      \n",
       "1  10055ff.jpg_Flower                                                      \n",
       "2  10055ff.jpg_Gravel  110 11 125 123 417 217 760 232 1099 251 1447 2...   \n",
       "3   10055ff.jpg_Sugar  3 184 201 4 215 129 352 345 701 347 1051 347 1...   \n",
       "4    6e54759.jpg_Fish                                                      \n",
       "\n",
       "                                     EncodedPixels_1  \\\n",
       "0                                                      \n",
       "1                                                      \n",
       "2  1587 1 1593 3 1603 4 1930 2 1933 7 1942 4 1952...   \n",
       "3  54 1 56 4 65 2 68 1 73 3 77 1 84 1 88 1 261 1 ...   \n",
       "4                                                      \n",
       "\n",
       "                                     EncodedPixels_2  \\\n",
       "0                                                      \n",
       "1                                                      \n",
       "2  1207 50 1553 67 1887 102 2236 104 2585 106 293...   \n",
       "3  7 134 251 77 355 148 590 96 704 154 937 104 10...   \n",
       "4                                                      \n",
       "\n",
       "                                     EncodedPixels_3  \\\n",
       "0                                                      \n",
       "1                                                      \n",
       "2  522 33 857 68 1203 90 1550 95 1870 2 1887 114 ...   \n",
       "3  5 136 242 90 353 150 586 103 702 155 929 113 1...   \n",
       "4                                                      \n",
       "\n",
       "                                     EncodedPixels_4  \\\n",
       "0                                                      \n",
       "1                                                      \n",
       "2  99 17 127 120 441 165 607 27 785 203 1124 228 ...   \n",
       "3  1 135 258 87 351 142 600 98 701 146 945 104 10...   \n",
       "4                                                      \n",
       "\n",
       "                                            ensemble  \n",
       "0                                                     \n",
       "1                                                     \n",
       "2  1207 50 1553 67 1887 102 2236 104 2585 106 293...  \n",
       "3  7 134 251 77 355 148 586 103 704 154 929 113 1...  \n",
       "4                                                     "
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sub_merged['ensemble'] = res\n",
    "sub_merged.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "EncodedPixels_0 0.4115738236884803\n",
      "EncodedPixels_1 0.4053542455381287\n",
      "EncodedPixels_2 0.39670091941590047\n",
      "EncodedPixels_3 0.39034613304488913\n",
      "EncodedPixels_4 0.4052866414277988\n",
      "ensemble 0.38115197404002166\n"
     ]
    }
   ],
   "source": [
    "for col in sub_merged.columns[1:]:\n",
    "    print(col, (sub_merged[col]!='').mean())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Image_Label</th>\n",
       "      <th>EncodedPixels</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10055ff.jpg_Fish</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10055ff.jpg_Flower</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10055ff.jpg_Gravel</td>\n",
       "      <td>1207 50 1553 67 1887 102 2236 104 2585 106 293...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10055ff.jpg_Sugar</td>\n",
       "      <td>7 134 251 77 355 148 586 103 704 154 929 113 1...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>6e54759.jpg_Fish</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          Image_Label                                      EncodedPixels\n",
       "0    10055ff.jpg_Fish                                                   \n",
       "1  10055ff.jpg_Flower                                                   \n",
       "2  10055ff.jpg_Gravel  1207 50 1553 67 1887 102 2236 104 2585 106 293...\n",
       "3   10055ff.jpg_Sugar  7 134 251 77 355 148 586 103 704 154 929 113 1...\n",
       "4    6e54759.jpg_Fish                                                   "
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "final_sub = sub_merged[['Image_Label', 'ensemble']]\n",
    "final_sub.rename(columns={'ensemble': 'EncodedPixels'}, inplace=True)\n",
    "final_sub.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [],
   "source": [
    "final_sub.to_csv('../submission/dirty_ensemble.csv.gz', index=False, compression='gzip')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [],
   "source": [
    "# _df = pd.read_csv('../submission/6671_1117_fpn_resnet34_576x768_v1_seed2120-21-22-23-24.csv.gz').fillna('')\n",
    "# _df.to_csv('../submission/bin/6671_1117_fpn_resnet34_576x768_v1_seed2120-21-22-23-24.csv', index=False)\n",
    "# _df.head(5)\n",
    "#(_df.EncodedPixels!='').mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [],
   "source": [
    "i = 30"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "35\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAl0AAABWCAYAAAD12iPBAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjAsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+17YcXAAAO+UlEQVR4nO3dfYwc9X3H8ffnfOZs4wfsYBxsg0MSgwIodVIDkagUqaR1SNVCK5qSP9r0ESoVIaT+UQf+KGqVtqn6ENpEjZwmUqjauFETVFciNQQpUlBig0FgMMbExm5sDmwe4gfAT+f79o+Zjefudvb29nZmd3Y+L+l0u/OwM/O53/z2e/Owq4jAzMzMzIo11OsVMDMzM6sDF11mZmZmJXDRZWZmZlYCF11mZmZmJXDRZWZmZlYCF11mZmZmJXDRZWZmZrUg6XclPd5i/Pcl/WFRy3fRZWZmZlYCF11mZmZmJXDRZWZmZl0jaaWkb0t6XdJ+SXenw++X9C1JD0o6IWmXpPWZ+f5M0ivpuD2SbkqHD0naKGmfpDfT11iWjnufpJD0e5IOSvqppD+WdJ2knZKOSvrS1FXUP0s6JunFxnJytuX3Je1OX3erpDWzycZFl5mZmXWFpCHgf4BngVXATcA9kjakk/wasBm4CNgCfCmd7yrgLuC6iFgEbAAOpPPcDdwKfBxYCfwU+PKkRd8ArAV+C/gicB/wCeAa4NOSPj5p2peBi4E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g2UAh+TibGrQdZ5PP2bTmPidfQW1noplc1V/ED8nV/y+R3P1wX6/Xp8NteD/JHRvPArsa2wG8B3gM+HH6e1k6XMCX021+DljvbOqXTVH5OJvBbzvOxtn0Uz7OpnXbyf74a4DMzMzMStDr04tmZmZmteCiy8zMzKwELrrMzMzMSuCiy8zMzKwELrrMzMzMSuCiy8zMzKwELrrMzMzMSvD/IYLnQbcR//YAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 720x576 with 8 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "i += 1\n",
    "print(i)\n",
    "\n",
    "fig = plt.figure(figsize=(10, 8))\n",
    "\n",
    "rles = sub_merged.loc[i, ][1:]\n",
    "\n",
    "for idx, rle in enumerate(rles):\n",
    "    if rle=='':\n",
    "        _mask = np.zeros((350, 525), dtype=np.float)\n",
    "    else:\n",
    "        _mask = rle2mask(rle, (525, 350))\n",
    "    ax = fig.add_subplot(1, len(rles), idx+1)\n",
    "    plt.imshow(_mask)\n",
    "    if idx==len(rles)-1:\n",
    "        plt.title('ensemble')\n",
    "#print('Done')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Image_Label</th>\n",
       "      <th>EncodedPixels</th>\n",
       "      <th>img_id</th>\n",
       "      <th>has_mask</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10055ff.jpg_Fish</td>\n",
       "      <td></td>\n",
       "      <td>10055ff</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10055ff.jpg_Flower</td>\n",
       "      <td></td>\n",
       "      <td>10055ff</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10055ff.jpg_Gravel</td>\n",
       "      <td>1217 23 1560 2 1563 34 1599 2 1618 4 1623 2 18...</td>\n",
       "      <td>10055ff</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          Image_Label                                      EncodedPixels  \\\n",
       "0    10055ff.jpg_Fish                                                      \n",
       "1  10055ff.jpg_Flower                                                      \n",
       "2  10055ff.jpg_Gravel  1217 23 1560 2 1563 34 1599 2 1618 4 1623 2 18...   \n",
       "\n",
       "    img_id  has_mask  \n",
       "0  10055ff         0  \n",
       "1  10055ff         0  \n",
       "2  10055ff         1  "
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#fname = '../submission/6609_1109_deeplabv3plus_resnet_512x768_v10_seed2021.csv.gz'\n",
    "fname = '../submission/dirty_ensemble.csv.gz'\n",
    "#fname = '../data/raw/train.csv'\n",
    "\n",
    "sub = pd.read_csv(fname).fillna('')\n",
    "sub['img_id'] = [name.split('.')[0] for name in sub.Image_Label]\n",
    "sub['has_mask'] = (sub.EncodedPixels!='').astype(np.int)\n",
    "sub.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "agg = sub.groupby(['img_id'])['has_mask'].agg(['sum'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2    1793\n",
       "1    1449\n",
       "3     366\n",
       "0      81\n",
       "4       9\n",
       "Name: sum, dtype: int64"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agg['sum'].value_counts()# / 3698"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2    0.427696\n",
       "3    0.281284\n",
       "1    0.243058\n",
       "4    0.047962\n",
       "Name: sum, dtype: float64"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## the train.csv\n",
    "agg['sum'].value_counts() / 5546"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### post process"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [],
   "source": [
    "def post_process(mask, min_size):\n",
    "    \"\"\"\n",
    "    Post processing of each predicted mask, components with lesser number of pixels\n",
    "    than `min_size` are ignored\n",
    "    \"\"\"\n",
    "    #mask = cv2.threshold(probability, threshold, 1, cv2.THRESH_BINARY)[1]\n",
    "    num_component, component = cv2.connectedComponents(mask.astype(np.uint8))\n",
    "    predictions = np.zeros((350, 525), np.float32)\n",
    "    num = 0\n",
    "    for c in range(1, num_component):\n",
    "        p = (component == c)\n",
    "        if p.sum() > min_size:\n",
    "            predictions[p] = 1\n",
    "            num += 1\n",
    "    return predictions, num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Image_Label</th>\n",
       "      <th>EncodedPixels</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10055ff.jpg_Fish</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10055ff.jpg_Flower</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10055ff.jpg_Gravel</td>\n",
       "      <td>1217 12 1564 20 1599 2 1618 4 1910 72 2253 4 2...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10055ff.jpg_Sugar</td>\n",
       "      <td>10 123 264 58 356 144 592 90 704 150 939 98 10...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>6e54759.jpg_Fish</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          Image_Label                                      EncodedPixels\n",
       "0    10055ff.jpg_Fish                                                   \n",
       "1  10055ff.jpg_Flower                                                   \n",
       "2  10055ff.jpg_Gravel  1217 12 1564 20 1599 2 1618 4 1910 72 2253 4 2...\n",
       "3   10055ff.jpg_Sugar  10 123 264 58 356 144 592 90 704 150 939 98 10...\n",
       "4    6e54759.jpg_Fish                                                   "
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sub = pd.read_csv('../submission/dirty_ensemble.csv.gz').fillna('')\n",
    "sub.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.4200919415900487"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(sub.EncodedPixels!='').mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 1min 36s, sys: 4.84 s, total: 1min 41s\n",
      "Wall time: 1min 4s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "for idx in sub.index:\n",
    "    rle = sub.loc[idx, 'EncodedPixels']\n",
    "    if str(rle)!='':\n",
    "        mask = rle2mask(rle, (525, 350))\n",
    "        predictions, num = post_process(mask, min_size=5000)#20000\n",
    "        if num==0:\n",
    "            sub.loc[idx, 'EncodedPixels'] = ''\n",
    "        else:\n",
    "            sub.loc[idx, 'EncodedPixels'] = mask2rle(predictions)\n",
    "    else:\n",
    "        continue"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.41928069226608977"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(sub.EncodedPixels!='').mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [],
   "source": [
    "sub.to_csv('../submission/dirty_ensemble_with_postprocess.csv.gz', index=False, compression='gzip')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#idx = 5\n",
    "idx += 1\n",
    "rle = sub.loc[idx, 'EncodedPixels']\n",
    "mask = rle2mask(rle, (525, 350))\n",
    "predictions, num = post_process(mask, min_size=20000)#15000\n",
    "num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x7f0df5eed8d0>"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAW4AAAD8CAYAAABXe05zAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjAsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+17YcXAAAScklEQVR4nO3dbaycZZ3H8e9voRTxCatC+pQtat2AGy2mAQz7QkW3SMxWEzWQjXZNk+4LTHRjsgtusqvJmuhGxTVuyNZAxI0rsquGxhARK8aYrEDByoMVrUqktqG7guBqrID/fTH3waFOe+acM9M518z3k0zmvq/7npnrOhx+vc5/rnsmVYUkqR1/NOkOSJIWxuCWpMYY3JLUGINbkhpjcEtSYwxuSWrM2II7ycVJ7k+yP8kV43odSZo1Gcc67iQnAT8AXg8cAO4ALquq7438xSRpxoxrxn0esL+qflxVvwWuB7aO6bUkaaacPKbnXQs82Ld/ADj/WCefkpV1Ks8cU1ckqT2/4Vf8to5k0LFxBfegF3taTSbJDmAHwKmcxvm5aExdkaT23Fa7j3lsXKWSA8D6vv11wMH+E6pqZ1VtrqrNK1g5pm5I0vQZV3DfAWxMclaSU4BLgV1jei1JmiljKZVU1RNJ3gXcDJwEXFtV943jtSRp1oyrxk1V3QTcNK7nl6RZ5ZWTktQYg1uSGmNwS1JjDG5JaozBLUmNMbglqTEGtyQ1xuCWpMYY3JLUGINbkhpjcEt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qj7Wd2iqx57khd1MmyTPAF5H7826W4G3dKcdPe65n8dbgK9XV/hsSVVdWVXrqmoDvf+Hv15Vf8mUjxsgyTOTPHtuG/hz4F7G/bu+DAr7lwA/oFcL/PtJ92fEY/sccAh4nN6/tNvp1fJ2Az/s7ld154beCpsfAfcAmyfd/yWM+8/o/fl3N7C3u10y7WMHXg58pxv3vcA/dO0vAm4H9gP/Cazs2k/t9vd3x1806TGM4GfwauDLszLubozf7W73zWXYuH/XvXJSkhoz6VKJJGmBDG5JaozBLUmNMbglqTEGtyQ1xuCWpMYY3JLUGINbkhrz/53onovyp/FoAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.imshow(mask)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x7f0df5e4af28>"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAW4AAAD8CAYAAABXe05zAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjAsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+17YcXAAASU0lEQVR4nO3df6yk1V3H8fdHWBbpDyltIcvuxkXdGqixi7kBDP7RltalpHFrUgzEtGuzyfoHTahpoqCJPxJNqtFSmxoiDaTU1ALaNmwIEXFLY0wUutDl55ayraSsu2G1/Ko2XYF+/WOei8Myu3funZmde2ber2Qyz3OeM3PPub39cPZ7zzM3VYUkqR0/Nu0BSJKWx+CWpMYY3JLUGINbkhpjcEtSYwxuSWrMxII7yaVJHk+yP8k1k/o6kjRvMol93ElOAr4FvBc4AHwduLKqH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WVS8kg6bY6zqgrcm5V9XLwJYkpwNfAc4d1K17nol5J3k/cLiq7k/yzsXmAV1nat59Lq6qg0nOBO5O8s3j9B3L3Ke94j4AbOw73wAcnNJYTpSnk6wD6J4Pd+0z9b1IsoZeaH+hqr7cNc/F3AGq6jnga/Rq/KcnWVwk9c/tlXl3138CeObEjnQsLgZ+JcmTwC30yiWfYvbnDUBVHeyeD9P7j/UFTPhnfdrB/XVgc/fb51OAK4BdUx7TpO0CtnfH2+nVfxfbP9z91vki4PnFf2q1Jr2l9Y3Avqr6ZN+lmZ57krd2K22S/DjwHnq/rLsH+GDX7eh5L34/Pgh8tbrCZ0uq6tqq2lBVm+j9f/irVfXrzPi8AZK8LskbFo+BXwYeYdI/66ugsH8Z8C16tcDfm/Z4xjy3LwKHgBfp/Zd2B71a3m7gie75jK5v6O2w+TbwMLAw7fGPMO9fovfPv4eAvd3jslmfO/DzwDe6eT8C/H7X/lPAfcB+4O+AtV37qd35/u76T017DmP4HrwTuGNe5t3N8cHu8ehihk36Z907JyWpMdMulUiSlsnglqTGGNyS1BiDW5IaY3BLUmMMbklqjMEtSY0xuCWpMf8HiPyGG5uqJ34AAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.imshow(predictions)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
